<script type="text/javascript" src="{$smarty.const.URL_JS}/JsHttpRequest.js"></script>
<script type="text/javascript" src="{$smarty.const.URL_JS}/widget_checkout.js"></script>
<div style="padding: 6px;">

<h1 class="breadcrumbs"><a href='{$olist_url}'>{lbl_ordr_order_list}</a>
&raquo;
<a href="{$odet_url}">{"ordr_order"|translate} #{$order_info.orderID_view}</a>
&raquo;
{"lbl_edit_order"|translate}
</h1>

<form name="OrderForm" method="post">
<input type="hidden" name="action" value="save_order_info">
<input type="hidden" name="order_id" value="{$order_info.orderID}">

{* CUSTOMER INFO *}
{assign var="customer_full_name" value=$order_info.customer_firstname|cat:" "|cat:$order_info.customer_lastname}
<table border="0" width="630" cellpadding="5" cellspacing="0" style="background-color: #FAFAE7;">
<colgroup>
    <col width="10%" />
    <col width="90%" />
</colgroup>
<tr>
    <td valign="top">{"ordr_customer"|translate}:</td>
    <td>
        <a href="index.php?ukey=user_info&userID={$order_info.customerID}"><span id="ord_customer_name">{$customer_full_name|escape:'html'}</span></a>
        ({"usr_custinfo_login"|translate|lower}:&nbsp;{if $customer_login}<strong>{$customer_login}</strong>{else}{"lbl_n_a"|translate}{/if})
        <br />
        {if $smarty.const.CONF_BACKEND_SAFEMODE eq 0}
            {"usr_custinfo_email"|translate}:&nbsp;<a href="mailto:{$order_info.customer_email|escape:'html'}">{$order_info.customer_email|escape:'html'}</a>
        {else}
            <b>{"msg_safemode_info_blocked"|translate}</b>
        {/if}
        {section name=i loop=$order_info.reg_fields_values}
         ,&nbsp;
         {$order_info.reg_fields_values[i].reg_field_name|escape:'html'}: {$order_info.reg_fields_values[i].reg_field_value|escape:'html'}
        {/section}
    </td>
</tr>
<tr>
    <td colspan="2">
        <button type="button" onClick="onAssignClick();">{"btn_assign_to_customer"|translate}</button>
        {"lbl_or"|translate}&nbsp;<a href="index.php?ukey=user_info&userID={$order_info.customerID}">{"lbl_edit_cust_info"|translate|replace:"%0%":$customer_full_name}</a>
    </td>
</tr>
</table>

<br />

<table border="0" cellpadding="2" cellspacing="1">
<tr>
    <td valign="top">{"ordr_comment"|translate}:</td>
    <td><textarea name="order_comment" rows="3" cols="50">{$order_info.customers_comment|escape:'html'}</textarea></td>
</tr>
</table>

<br />

<table cellpadding="0" cellspacing="0" border="0" width="630">
    <colgroup>
        <col width="50%" />
        <col width="50%" />
    </colgroup>
<tr>
{* SHIPPING INFO *}
    <td style="padding-right: 15px;" valign="top">
        <h3>{lbl_shipping} &ndash; <input type="text" size="20" maxlength="255" name="shipping[type]" value="{$order_info.shipping_type|escape:'html'}" />{if $order_info.shippingServiceInfo} ({$order_info.shippingServiceInfo|escape:'html'}){/if}</h3>
        
        <table border="0" cellpadding="1" cellspacing="1" width="100%" class="order_edit_address_form">
            <colgroup>
                <col width="30%" />
                <col width="70%" />
            </colgroup>
            <tr>
                <td align="left" colspan="2">{"ordr_recipient"|translate}:</td>
            </tr>
            <tr>
                <td nowrap="nowrap">{"usr_custinfo_first_name"|translate}:</td>
                <td><input class="txt_or_sel" type="text" name="shipping[firstname]" size="17" maxlength="255" value="{$order_info.shipping_firstname|escape:'html'}" /></td>
            </tr>
            <tr>
                <td nowrap="nowrap">{"usr_custinfo_last_name"|translate}:</td>
                <td><input class="txt_or_sel" type="text" name="shipping[lastname]" size="17" maxlength="255" value="{$order_info.shipping_lastname|escape:'html'}" /></td>
            </tr>
            <tr>
                <td nowrap="nowrap" valign="top">{"str_address"|translate}:</td>
                <td><textarea class="txt_or_sel" name="shipping[address]" rows="2" cols="17">{$order_info.shipping_address|escape:'html'}</textarea></td>
            </tr>
            <tr>
                <td nowrap="nowrap">{"usr_custinfo_city"|translate}:</td>
                <td><input class="txt_or_sel" type="text" name="shipping[city]" size="17" maxlength="255" value="{$order_info.shipping_city|escape:'html'}" /></td>
            </tr>
            <tr>
                <td nowrap="nowrap">{"usr_custinfo_state"|translate}:</td>
                <td>
                {if $states.shipping}
                    {include file="backend/order_editor/states.html" states=$states.shipping selected=$shipping_state_id name='shipping[state_id]' class="txt_or_sel"}
                {else}
                    <input class="txt_or_sel" type="text" name="shipping[state]" size="17" maxlength="255" value="{$order_info.shipping_state|escape:'html'}" />
                {/if}
                </td>
            </tr>
            <tr>
                <td nowrap="nowrap">{"usr_custinfo_zip"|translate}:</td>
                <td><input class="txt_or_sel" type="text" name="shipping[zip]" size="17" maxlength="255" value="{$order_info.shipping_zip|escape:'html'}"></td>
            </tr>
            <tr>
                <td nowrap="nowrap">{"usr_custinfo_country"|translate}:</td>
                <td>
                {if $countries}
                    {html_options name="shipping[country_id]" options=$countries selected=$shipping_country_id onChange="changeStates('shipping')" class="txt_or_sel"}
                {else}
                    <input class="txt_or_sel" type="text" name="shipping[country]" size="17" maxlength="255" value="{$order_info.shipping_country|escape:'html'}" />
                {/if}
                </td>
            </tr>
        </table>
        <br />
    </td>
{* BILLING INFO *}
    <td valign="top">
        <h3>{lbl_payment} &ndash; <input type="text" size="20" maxlength="255" name="payment_type" value="{$order_info.payment_type|escape:'html'}" /></h3>
        
        <table border="0" cellpadding="1" cellspacing="1" width="100%" class="order_edit_address_form">
            <colgroup>
                <col width="30%" />
                <col width="70%" />
            </colgroup>
            <tr>
                <td align="left" colspan="2">{"ordr_payee"|translate}:</td>
            </tr>
            <tr>
                <td nowrap="nowrap">{"usr_custinfo_first_name"|translate}:</td>
                <td><input class="txt_or_sel" type="text" name="billing[firstname]" size="17" maxlength="255" value="{$order_info.billing_firstname|escape:'html'}" /></td>
            </tr>
            <tr>
                <td nowrap="nowrap">{"usr_custinfo_last_name"|translate}:</td>
                <td><input class="txt_or_sel" type="text" name="billing[lastname]" size="17" maxlength="255" value="{$order_info.billing_lastname|escape:'html'}" /></td>
            </tr>
            <tr>
                <td nowrap="nowrap" valign="top">{"str_address"|translate}:</td>
                <td><textarea class="txt_or_sel" name="billing[address]" rows="2" cols="17">{$order_info.billing_address|escape:'html'}</textarea></td>
            </tr>
            <tr>
                <td nowrap="nowrap">{"usr_custinfo_city"|translate}:</td>
                <td><input class="txt_or_sel" type="text" name="billing[city]" size="17" maxlength="255" value="{$order_info.billing_city|escape:'html'}" /></td>
            </tr>
            <tr>
                <td nowrap="nowrap">{"usr_custinfo_state"|translate}:</td>
                <td>
                {if $states.billing}
                    {include file="backend/order_editor/states.html" states=$states.billing selected=$billing_state_id name='billing[state_id]' class="txt_or_sel"}
                {else}
                    <input class="txt_or_sel" type="text" name="billing[state]" size="17" maxlength="255" value="{$order_info.billing_state|escape:'html'}" />
                {/if}
                </td>
            </tr>
            <tr>
                <td nowrap="nowrap">{"usr_custinfo_zip"|translate}:</td>
                <td><input class="txt_or_sel" type="text" name="billing[zip]" size="17" maxlength="255" value="{$order_info.billing_zip|escape:'html'}"></td>
            </tr>
            <tr>
                <td nowrap="nowrap">{"usr_custinfo_country"|translate}:</td>
                <td>
                {if $countries}
                    {html_options name="billing[country_id]" options=$countries selected=$billing_country_id onChange="changeStates('billing')" class="txt_or_sel"}
                {else}
                    <input class="txt_or_sel" type="text" name="billing[country]" size="17" maxlength="255" value="{$order_info.billing_country|escape:'html'}" />
                {/if}
                </td>
            </tr>
        </table>
        
        {if $order_info.cc_number || $order_info.cc_holdername || $order_info.cc_expires || $order_info.cc_expires}
        <p>
            <strong>{"ordr_ccinfo"|translate}</strong>
            <br />
            {if true}{*$https_connection_flag*}
                <table>
                <tr>
                    <td>{"ordr_ccnumber"|translate}: <b>{$order_info.cc_number|replace:"<":"&lt;"}</b></td>
                </tr>
                <tr>
                    <td>{"ordr_ccholdername"|translate}: <b>{$order_info.cc_holdername|replace:"<":"&lt;"}</b></td>
                </tr>
                <tr>
                    <td>{"ordr_ccexpires"|translate}: <b>{$order_info.cc_expires|replace:"<":"&lt;"}</b></td>
                </tr>
                <tr>
                    <td>{"ordr_cccvv"|translate}: <b>{$order_info.cc_cvv|replace:"<":"&lt;"}</b></td>
                </tr>
                </table>
            {else}
                {"ordr_ccinfo_shown_only_in_https"|translate}
                <br /><br />
            {/if}
        </p>
        {/if}
    </td>
</tr>
</table>

<div style="padding-left: 10px;">
<table class="grid" id="ord_order_content" border="0" width="620">

<tr class="gridsheader"> 
    <td></td>
    <td nowrap="nowrap" >{"prdset_product_name"|translate}</td>
    <td nowrap="nowrap" align="center">{"hdr_price_for_item"|translate}</td>
    <td nowrap="nowrap" align="center">{"cart_product_quantity"|translate}</td>
    <td nowrap="nowrap" align="center">{"ordr_tax"|translate}</td>
    <td nowrap="nowrap" align="right">{"ordr_itemprice_excluding_tax"|translate}</td>
</tr>
{if  $order_content}
{section name=i loop=$order_content}
<tr class="{cycle values='gridline,gridline1' name='ord_content'}">
    <td><a href="javascript: void(0);" onClick="onDeleteProductClick(this);"><img src="images_common/remove.gif" border="0"></a></td> 
    <td>
        <input type="hidden" name="order_content[{$smarty.section.i.index}][item_id]" value="{$order_content[i].itemID}" />
        <input type="hidden" name="order_content[{$smarty.section.i.index}][name]" value="{$order_content[i].name|escape:'html'}" />{$order_content[i].name|escape:'html'}
    </td>
    <td>{$order_info.currency_code}&nbsp;<input type="text" name="order_content[{$smarty.section.i.index}][price]" size="12" value="{$order_content[i].ItemBPrice|string_format:'%.2f'}" onBlur="onPriceChange(this);" /></td>
    <td align="center"><input type="text" name="order_content[{$smarty.section.i.index}][qty]" size="4" value="{$order_content[i].Quantity}" onBlur="onQtyChange(this);" /></td>
    <td align="right"><input type="text" name="order_content[{$smarty.section.i.index}][tax]" size="4" value="{$order_content[i].tax|string_format:'%.2f'}" onBlur="onPriceChange(this);" />%</td>
    <td align="right">{$order_info.currency_code}&nbsp;<span id="item_price_{$smarty.section.i.index}">{$order_content[i].ItemPrice|round:'2'|string_format:'%.2f'}</span></td>
</tr>
<script language="JavaScript" type="text/javascript">var no_products = false;</script>
{/section}
{else}
<tr>
    <td colspan="6" style="color: #777777">{"lbl_no_products_in_order"|translate}</td>
</tr>
<script language="JavaScript" type="text/javascript">var no_products = true;</script>
{/if}

{if $smarty.server.HTTP_USER_AGENT|strpos:'Safari'}
<tr style="height: 1px; background-color: #666666;"><td colspan="6" style="padding: 1px 0px 0px 0px;"></td></tr>
{else}
<tr style="height: 1px; background-color: #666666;"><td colspan="6" style="padding: 0px;"></td></tr>
{/if}

<tr style="background-color: #F5F0BB;">
    <td colspan="5">{"lbl_add_products_to_order"|translate}:&nbsp;<input type="text" id="search_products_field" size="24" value="" onkeypress="return find_noenter(event);" /></td>
    <td><button type="button" onClick="onFindProductsClick();">{"btn_find_products"|translate}</button></td>
</tr>

<tr class="gridline" id="finded_products" style="display: none;"><td colspan="6"></td></tr>

{if $smarty.server.HTTP_USER_AGENT|strpos:'Safari'}
<tr style="height: 1px; background-color: #666666;"><td colspan="6" style="padding: 1px 0px 0px 0px;"></td></tr>
{else}
<tr style="height: 1px; background-color: #666666;"><td colspan="6" style="padding: 0px;"></td></tr>
{/if}

<tr class="gridline1">
    <td colspan="5">
        <table cellpadding="0" cellspacing="0" width="100%">
        <colgroup>
            <col width="8%" />
            <col width="92%" />
        </colgroup>
        <tr>
            <td style="padding: 0px;">{"usr_custinfo_group_discount"|translate}</td>
            <td style="padding: 0px;">
                <span style="border-bottom: dashed 1px black; cursor: pointer;" onClick="var el = document.getElementById('tr_discount_descr'); el.style.display = (el.style.display == 'none' ? '' : 'none');">{"lbl_edit_discount_descr"|translate}</span>
            </td>
        </tr>
        </table>
    </td>
    <td align="right">{$order_info.currency_code}&nbsp;<input type="text" size="6" name="order_discount" value="{$order_info.cnv.order_discount|round:'2'|string_format:'%.2f'}" onBlur="onPriceChange(this);" /></td>
</tr>

<tr class="gridline1" id="tr_discount_descr" style="display: none;">
    <td colspan="6">
        <textarea name="order_discount_description" cols="50" rows="3">{$order_info.discount_description|escape:'html'}</textarea>
    </td>
    
</tr>

<tr class="gridline1">
    <td colspan="5">{"ordr_shipping_handling_cost"|translate}</td>
    <td align="right">{$order_info.currency_code}&nbsp;<input type="text" size="6" name="order_shipping_cost" value="{$order_info.cnv.shipping_cost|round:'2'|string_format:'%.2f'}" onBlur="onPriceChange(this);" /></td>
</tr>

<tr class="gridline1">
    <td colspan="5">{"ordr_tax"|translate}</td>
    <td align="right">{$order_info.currency_code}&nbsp;<span id="order_tax">{$order_info.tax|round:'2'|string_format:'%.2f'}</span></td>
</tr>

<tr class="gridline1" id="ord_total_row">
    <td colspan="5">{"str_total"|translate}</td>
    <td align="right">{$order_info.currency_code}&nbsp;<span id="order_total">{$order_info.cnv.order_amount|round:'2'|string_format:'%.2f'}</span></td>
</tr>

</table>

</div>

<table border="0" cellpadding="2" cellspacing="2">
    <tr>
        <td><button type="submit" style="font-size:130%; font-weight:bold; margin:10px; padding:3px;">{"btn_save"|translate}</button></td>
        <td>{"lbl_or"|translate}</td>
        <td><a href='{$odet_url}'>{"btn_cancel"|translate}</a></td>
    </tr>
</table>

</form>

</div>

<script language="JavaScript" type="text/javascript">
{literal}

function formatFloat(string)
{
	var sign = string.match(/(-)[\d]+\.?[\d]*/g);
	if(sign){
		sign='-';
	}else{
		sign = '';
	}
    string = string.replace(/[^0123456789.]/g,'');
    var parts = string.split('.');
    
    while(parts.length > 2)
    {
        parts[1] = parts[1].concat(parts[2]);
        parts.splice(2,1);
    };
    
    if(parts[0] == '')
    {
        parts[0] = '0';
    };
    
    if(parts.length !=2 || parts[1] == '')
    {
        parts[1] = '00';
    };
    
    if(parts[1].length == 1)
    {
        parts[1] += '0';
    };
    
    return ''+ sign + parts.join('.');
};

function formatInt(string, zero_avail)
{
    string = string.replace(/[^0123456789]/g,'');
    if(string == '')
    {
        string = '0';
    };
    
    if(string == '0' && !zero_avail)
    {
        string = '1';
    };
    
    return string;
};

function onPriceChange(el)
{
    var val = parseFloat(formatFloat(el.value));
    val = Math.round(val * 100) / 100;
    el.value = formatFloat(val.toString());
    recalcAll();
};

function onQtyChange(el)
{
    el.value = formatInt(el.value);
    recalcAll();
};

function recalcAll()
{
    var order_total = 0;
    var total_tax = 0;
    var frm = document.forms['OrderForm'];
    
    for(i=0; i<frm.elements.length; i++)
    {
        if(frm.elements[i].name.indexOf('order_content') == 0)
        {
            var el = frm.elements[i];
            if(el.name.indexOf('price') != -1)
            {
                var item_id = el.name.replace('order_content[','').replace('][price]','');
                var item_price = parseFloat(el.value) * parseInt(frm.elements['order_content['+item_id+'][qty]'].value);
                item_price = Math.round(item_price * 100) / 100;
                document.getElementById('item_price_'+item_id).innerHTML = formatFloat(item_price.toString());
                order_total += item_price;
                var item_tax_percent = parseFloat(frm.elements['order_content['+item_id+'][tax]'].value);
                var item_tax_value = item_price * item_tax_percent / 100;
                total_tax += item_tax_value;
            };
        }
        else if(frm.elements[i].name == 'order_discount')
        {
            order_total -= parseFloat(frm.elements[i].value);
        }
        else if(frm.elements[i].name == 'order_shipping_cost')
        {
            order_total += parseFloat(frm.elements[i].value);
        };
    };
    
    order_total += total_tax;
    order_total = Math.round(order_total * 100) / 100;
    document.getElementById('order_total').innerHTML = formatFloat(order_total.toString());
    
    total_tax = Math.round(total_tax * 100) / 100;
    document.getElementById('order_tax').innerHTML = formatFloat(total_tax.toString());
};

function getProductsCount()
{
    var frm = document.forms['OrderForm'];
    var count = 0;
    
    for(i=0; i<frm.elements.length; i++)
    {
        if(frm.elements[i].name.indexOf('order_content') == 0 && frm.elements[i].name.indexOf('price') != -1)
        {
            count++;
        };
    };
    
    return count;
};

var item_count = getProductsCount();

function onDeleteProductClick(el)
{
    if(getProductsCount() == 1)
    {
        alert('{/literal}{"err_cant_delete_last_product"|translate}{literal}');
        return;
    };
    el.parentNode.parentNode.parentNode.deleteRow(el.parentNode.parentNode.rowIndex);
    recalcAll();
};

function onFindProductsClick()
{
    var search_string = document.getElementById('search_products_field').value;
    if(search_string.length < 2) // TODO: make constant
    {
        document.getElementById('finded_products').style.display = '';
        document.getElementById('finded_products').childNodes[0].innerHTML = '{/literal}{"wrn_too_short_string"|translate}{literal}';
        return;
    };
    
    var req = new JsHttpRequest();
    
    req.onreadystatechange = function()
    {
        if (req.readyState != 4) return;
        if(req.responseText) alert(req.responseText);
        
        var products = req.responseJS.products;
        if(products.length == 0)
        {
            document.getElementById('finded_products').style.display = '';
            document.getElementById('finded_products').childNodes[0].innerHTML = '{/literal}{"msg_no_products_found"|translate}{literal}';
            return;
        };
        
        var _html = '<table border="0" cellpadding="2" cellspacing="1" width="100%">';
        _html += '<colgroup><col width="50%" /><col width="30%" /><col width="20%" /></colgroup>';
        
        for(i=0; i<products.length; i++)
        {
            _html += '<tr>';
            if(products[i].in_stock <= 0 && {/literal}{$smarty.const.CONF_CHECKSTOCK}{literal})
            {
                _html += '<td>'+products[i].name+'</td>';
            }
            else
            {
            _html += '<td><a href="javascript: void(0);" onClick="addProduct({\'name\': \''+escape(products[i].name.replace(/\"/g,'\\\"'))+'\', \'price\': \''+products[i].price+'\', \'product_id\': \''+products[i].product_id+'\', \'have_options\': \''+products[i].have_options+'\'});" style="text-decoration: underline;">'+products[i].name+'</a></td>';
            };
            _html += '<td>{/literal}{$order_info.currency_code}{literal}&nbsp;'+formatFloat(products[i].price)+'</td>';
            {/literal}
            {if $smarty.const.CONF_CHECKSTOCK}
            _html += '<td nowrap="nowrap">{"str_in_stock"|transcape}:&nbsp;'+products[i].in_stock+'</td>';
            {else}
            _html += '<td>&nbsp;</td>';
            {/if}
            {literal}
            _html += '</tr>';
        };
        
        _html += '</table>';
        
        var results_container = document.getElementById('finded_products');
        results_container.style.display = '';
        results_container.childNodes[0].innerHTML = _html;
    };

    try
    {
        req.open(null, set_query('&caller=1&initscript=ajaxservice'), true);
        req.send({'action': 'search_products', 'search_string': search_string, order_id: {/literal}{$order_info.orderID}{literal}});
    }
    catch ( e )
    {
      catchResult(e);
    }
    finally { ;}
};

function addProduct(product_info)
{
    if(product_info.have_options == '1')
    {
        openFadeIFrame(set_query('&action=show_options_form&product_id='+product_info.product_id));
        resizeFadeIFrame(500, 400);
        return;
    };

    var tbl = document.getElementById('ord_order_content');
    
    if(no_products)
    {
        tbl.deleteRow(1);
        var new_row_index = 1;
        no_products = false;
    }
    else
    {
        var new_row_index = getProductsCount()+1;
    };

    item_count++;
    
    var row = tbl.insertRow(new_row_index);
    row.className = (tbl.rows[new_row_index-1].className == 'gridline' ? 'gridline1' : 'gridline');
    
    var cell0 = row.insertCell(0);
    var cell1 = row.insertCell(1);
    var cell2 = row.insertCell(2);
    var cell3 = row.insertCell(3);
    var cell4 = row.insertCell(4);
    var cell5 = row.insertCell(5);
    cell0.innerHTML = '<a href="javascript: void(0);" onClick="onDeleteProductClick(this);"><img src="images_common/remove.gif" border="0"></a>';
    cell1.innerHTML = '<input type="hidden" name="order_content['+item_count+'][product_id]" value="'+product_info.product_id+'" /><input id="order_content_name_'+item_count+'" type="hidden" name="order_content['+item_count+'][name]" value="_" />'+unescape(product_info.name).replace(/\\\"/g,'\"');
    cell2.innerHTML = '{/literal}{$order_info.currency_code}{literal}&nbsp;<input type="text" name="order_content['+item_count+'][price]" size="12" value="'+formatFloat(product_info.price)+'" onBlur="onPriceChange(this);" />'
    cell3.innerHTML = '<input type="text" name="order_content['+item_count+'][qty]" size="4" value="1" onBlur="onQtyChange(this);" />';
    cell4.innerHTML = '<input type="text" name="order_content['+item_count+'][tax]" size="4" value="0.00" onBlur="onPriceChange(this);" />%';
    cell5.innerHTML = '{/literal}{$order_info.currency_code}{literal}&nbsp;<span id="item_price_'+item_count+'">'+formatFloat(product_info.price)+'</span>';
    var cell_value = document.getElementById('order_content_name_'+item_count);
    if(cell_value){
    	cell_value.value=unescape(product_info.name).replace(/\\\"/g,'\"');
    }
    
    cell3.style.textAlign = 'center';
    cell4.style.textAlign = 'right';
    cell5.style.textAlign = 'right';
    
    recalcAll();
};

{/literal}
var ORIG_URL = '{$smarty.const.CONF_FULL_SHOP_URL}';
{literal}

function onAssignClick()
{
    openFadeIFrame(set_query('&action=show_customer_search'));
    resizeFadeIFrame(500, 400);
};

function changeStates(states_type)
{
    var frm = document.forms['OrderForm'];
    var country_id  = frm.elements[states_type+'[country_id]'].value;
    
    var req = new JsHttpRequest();
    
    req.onreadystatechange = function()
    {
        if (req.readyState != 4) return;
        if(req.responseText) alert(req.responseText);
        
        var states = req.responseJS.states;
        
        var states_el = frm.elements[states_type+'[state_id]'];
        if(states.length > 0)
        {
            if(!states_el)
            {
                states_el = frm.elements[states_type+'[state]'];
                var pn = states_el.parentNode;
                pn.removeChild(states_el);
                
                var dd = document.createElement('SELECT');
                dd.name = states_type+'[state_id]';
                dd.className = 'txt_or_sel';
                
                pn.appendChild(dd);
                states_el = frm.elements[states_type+'[state_id]'];
            }
            else
            {
                while(states_el.options.length > 0)
                {
                    states_el.remove(0);
                };
            };
        }
        else
        {
            if(states_el)
            {
                var pn = states_el.parentNode;
                pn.removeChild(states_el);
                
                var inp = document.createElement('INPUT');
                inp.type = 'text';
                inp.className = 'txt_or_sel';
                inp.name = states_type+'[state]';
                
                pn.appendChild(inp);
                states_el = frm.elements[states_type+'[state]'];
            }
        };
        
        for(i=0; i<states.length; i++)
        {
            var opt = new Option();
            opt.value = states[i].zoneID;
            opt.text = states[i].zone_name;
            try
            {
                states_el.add(opt,null); // standards compliant
            }
            catch(ex)
            {
                states_el.add(opt); // IE only
            };        
        };
    };

    try
    {
        req.open(null, set_query('&caller=1&initscript=ajaxservice'), true);
        req.send({'action': 'ajax_get_states', 'country_id': country_id});
    }
    catch ( e )
    {
      catchResult(e);
    }
    finally { ;}
};

function find_noenter(event)
{
    if(event.keyCode == 13)
    {
        onFindProductsClick();
        return false;
    };
};

document.getElementById('content').style.backgroundColor = 'white';
{/literal}
</script>